package com.android.boncen.commonFrame;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.boncen.commonFrame.bean.UserBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 功能描述:
 *
 * @date 2017/2/9
 */

public class LoginActivity extends FragmentActivity {
    @BindView(R.id.etName)
    EditText etName;
    @BindView(R.id.etPWD)
    EditText etPWD;

    private ProgressDialog waitDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        waitDialog = new ProgressDialog(this);
        waitDialog.setMessage("正在处理");

    }

    @OnClick({R.id.btLogin, R.id.tvReg})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btLogin:
                waitDialog.show();
                login(etName.getText().toString(),etPWD.getText().toString());
                break;
            case R.id.tvReg:
                startActivity(new Intent(LoginActivity.this,RegistActivity.class));
                finish();
                break;
        }
    }
    
    private void login(String name,String pwd)
    {
        UserBean user = new UserBean();
        user.setUsername(name);
        user.setPassword(pwd);
        user.login(new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser bmobUser, BmobException e) {
                waitDialog.dismiss();
                if(e==null){

                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                    finish();
                }else{
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
